package com.fallsvc.onlinemusic.controller;

import com.fallsvc.onlinemusic.mapper.LoveMusicMapper;
import com.fallsvc.onlinemusic.model.LoveMusic;
import com.fallsvc.onlinemusic.model.Music;
import com.fallsvc.onlinemusic.model.User;
import com.fallsvc.onlinemusic.tools.Constant;
import com.fallsvc.onlinemusic.tools.ResponseBodyMessage;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther falls_vc
 * description:
 * @date 2025/4/9  20:58
 */
@Slf4j
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Resource
    private LoveMusicMapper loveMusicMapper;


    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest req){
        int musicId=Integer.parseInt(id);
        log.info("收藏音乐：id:"+musicId);

        // 没有session 不创建
        HttpSession session=req.getSession(false);
        if(session==null||session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            log.info("没有登录，导致喜欢失败");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        LoveMusic loveMusic=new LoveMusic();
        loveMusic.setMusicId(musicId);
        loveMusic.setUserId(user.getId());

        LoveMusic ret = loveMusicMapper.findLoveMusicByUserIdAndMusicId(loveMusic);
        if(ret!=null){
//            //TODO 取消收藏
//            boolean b = loveMusicMapper.deleteByUserIdAndMusicId(loveMusic);
//            if(b==true){
//                log.info("取消音乐收藏；{}",loveMusic);
//                return new ResponseBodyMessage<>(0,"取消收藏音乐成功",true);
//            }else{
//                return new ResponseBodyMessage<>(-1,"取消收藏音乐失败",false);
//            }
            return new ResponseBodyMessage<>(2,"音乐已经收藏",true);
        }

        boolean b=loveMusicMapper.insertLoveMusic(loveMusic);
        if(b==true){
            log.info("收藏音乐成功：{}",loveMusic);
            return new ResponseBodyMessage<>(0,"收藏音乐成功",true);
        }else{
            return new ResponseBodyMessage<>(-1,"收藏音乐失败",false);
        }
    }

    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false)String musicName,HttpServletRequest request){

        HttpSession session=request.getSession();

        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getId();
        log.info("找喜欢音乐的id："+userId);

        List<Music> retList=null;

        // TODO 异常处理
        if(StringUtils.hasLength(musicName)){
            retList=loveMusicMapper.findLoveMusicByKeyAndUserId(musicName,userId);
        }else{
            retList=loveMusicMapper.findLoveMusicByUserId(userId);
        }

        return new ResponseBodyMessage<>(0,"查询所有歌曲信息",retList);

    }
    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deletelovemusic(@RequestParam String id,HttpServletRequest request){
        int musicId =Integer.parseInt(id);

        HttpSession session=request.getSession();
        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getId();

        LoveMusic loveMusic=new LoveMusic();
        loveMusic.setMusicId(musicId);
        loveMusic.setUserId(userId);
        log.info("取消收藏音乐：{}",loveMusic);
        Boolean ret=loveMusicMapper.deleteByUserIdAndMusicId(loveMusic);
        if(ret==false) return new ResponseBodyMessage<>(-1,"取消收藏失败",ret);

        return new ResponseBodyMessage<>(0,"取消收藏成功",ret);
    }
}
